package com.lyy.photovoltaic.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lyy.photovoltaic.beans.Equipments;
import com.lyy.photovoltaic.beans.UserCartDetails;
import com.lyy.photovoltaic.common.utils.MyRandom;
import com.lyy.photovoltaic.common.vo.ResultCode;
import com.lyy.photovoltaic.common.vo.ResultVO;
import com.lyy.photovoltaic.mapper.UserCartDetailsMapper;
import com.lyy.photovoltaic.service.UserCartDeatilsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class UserCartDetailsServiceImpl implements UserCartDeatilsService {
    @Autowired
    UserCartDetailsMapper userCartDetailsMapper;

    @Override
    public ResultVO addUserCartDetails(UserCartDetails userCartDetails) {
//        System.out.println(userCartDetails);
        String uCId = userCartDetails.getUserCartId();
        String eId = userCartDetails.geteId();
//        System.out.println(uCId);
        String uCDId ="uCDId" +eId + uCId;
//        System.out.println(uCDId);
        UserCartDetails userCartDetails2 = new UserCartDetails();
        userCartDetails2.setUserCartDetailsId(uCDId);
        userCartDetails2.seteId(userCartDetails.geteId());
        userCartDetails2.seteCount(userCartDetails.geteCount());
        userCartDetails2.seteRealprice(userCartDetails.geteRealprice());
        userCartDetails2.setePrice(userCartDetails.getePrice());
        userCartDetails2.setUserCartId(uCId);
        UserCartDetails userCartDetails0 = userCartDetailsMapper.selectByPrimaryKey(uCDId);
        if (userCartDetails0==null) {
            int i = userCartDetailsMapper.insert(userCartDetails2);
            if (i > 0) {
                ResultVO resultVO = new ResultVO(ResultCode.SUCCESS,"加入购物车列表成功",userCartDetails2);
                return resultVO;
            }
            return new ResultVO(ResultCode.FAILED, "加入购物车列表失败", null);
        }else {
            int k = userCartDetailsMapper.updateByPrimaryKey(userCartDetails2);
            if (k > 0) {
                ResultVO resultVO = new ResultVO(ResultCode.SUCCESS,"更新购物车列表成功",userCartDetails2);
                return resultVO;
            }
            return new ResultVO(ResultCode.FAILED, "更新购物车列表失败", null);
        }
    }

    @Override
    public ResultVO getUserCartDetailsByUCID(String uCId) {
        Example example = new Example(UserCartDetails.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userCartId",uCId);
        List<UserCartDetails> userCartDetails = userCartDetailsMapper.selectByExample(example);
        if (userCartDetails != null) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查询未支付的购物车详情列表",userCartDetails);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "查询未支付的购物车详情列表失败", null);
    }

    @Override
    public ResultVO deleteUserCartDetails(String uCDId) {

        int i = userCartDetailsMapper.deleteByPrimaryKey(uCDId);
        if (i > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "删除购物车中的商品成功",null);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "购物车中已不存在该商品",null);
    }

    @Override
    public ResultVO batchDeleteUserCartDetails(String[] eIds) {
//        System.out.println(eIds);
        List<String> strings = Arrays.asList(eIds);
        System.out.println(strings);
        int i = userCartDetailsMapper.deleteByBatch(eIds);
        if (i > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "删除成功",null);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "删除失败",null);
    }

    @Override
    public ResultVO updateUserCartDetailsCount(String uCDId,int num) {
        UserCartDetails userCartDetails = new UserCartDetails();
        userCartDetails.setUserCartDetailsId(uCDId);
        userCartDetails.seteCount(num);
        int i = userCartDetailsMapper.updateByPrimaryKeySelective(userCartDetails);
        if (i > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "更新购车详情商品数量成功",userCartDetails);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "更新失败", null);
    }

    @Override
    public ResultVO batchAddUserCartDetails(JSONObject jsonObject) {
        System.out.println(jsonObject);
        JSONArray jsonArrays = jsonObject.getJSONArray("data");
        List<UserCartDetails> list = new ArrayList<>();
        //通过遍历将json数据中的数据转化成实体类
        for (Object jsonArray : jsonArrays) {
            //会用JSONObject包中的函数将字符串转化成实体类
            UserCartDetails userCartDetails = (UserCartDetails) JSONObject.toJavaObject((JSONObject) jsonArray, UserCartDetails.class);
            list.add(userCartDetails);
        }
        System.out.println(list);
//        userCartDetailsMapper.batchInsert();
        return null;

    }
}
